<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="商铺标题" prop="title">
        <el-input
          v-model="queryParams.title"
          clearable
          placeholder="请输入商铺标题"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属栏目" prop="columnType">
        <el-select v-model="queryParams.columnType" clearable placeholder="请选择所属栏目" style="width: 100%">
          <el-option
            v-for="item in columnType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属类别" prop="classType">
        <el-select v-model="queryParams.classType" clearable placeholder="请选择所属类别" style="width: 100%">
          <el-option
            v-for="item in classType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属区域" prop="region">
        <el-select v-model="queryParams.region" clearable placeholder="请选择所属区域" style="width: 100%">
          <el-option
            v-for="item in regionType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否审核" prop="region">
        <el-select v-model="queryParams.review" clearable placeholder="请选择审核状态">
          <el-option
            v-for="dict in dict.type.xds_review"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:shop:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:shop:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:shop:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:shop:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" border height="650" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="序号" type="index" width="50"/>
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="商铺标题" prop="title" show-overflow-tooltip width="200"/>
      <el-table-column align="center" label="商铺类型" prop="className"/>
      <el-table-column align="center" label="所属栏目" prop="columnName"/>
      <el-table-column align="center" label="是否审核" prop="review">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xds_review" :value="scope.row.review"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="浏览量" prop="pageViews"/>
      <el-table-column align="center" label="所属区域" prop="regionName"/>
      <el-table-column align="center" label="地址" prop="address" show-overflow-tooltip/>
      <el-table-column align="center" label="联系人" prop="contacts"/>
      <el-table-column align="center" label="联系方式" prop="phone" show-overflow-tooltip width="120"/>
      <el-table-column align="center" label="装修程度" prop="decoration"/>
      <el-table-column align="center" label="租金" prop="rent"/>
      <el-table-column align="center" label="面积(㎡)" prop="area"/>
      <el-table-column align="center" label="缩略图" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :height="30" :src="scope.row.img" :width="30"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="图片相册" prop="imgurl" width="100">
        <template slot-scope="scope">
          <image-preview :height="30" :src="scope.row.imgurl" :width="30"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="发布时间" prop="releaseDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="经度" prop="longitude" width="120"/>
      <el-table-column align="center" label="维度" prop="dimension" width="120"/>

      <el-table-column align="center" label="附近地标" prop="landmarks" show-overflow-tooltip width="120"/>
      <el-table-column align="center" label="配套设施" prop="facilities"/>
      <el-table-column align="center" label="描述" prop="remark" show-overflow-tooltip/>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:shop:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['system:shop:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
      <div slot="empty" style="text-align: left;">
        <el-empty description="暂时没有数据，快去添加一个吧！"/>
      </div>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改商铺管理对话框 -->
    <el-drawer
      :before-close="cancel"
      :title="title"
      :visible.sync="open"
      direction="rtl"
      size="50%">
      <div style="height: 95%; overflow-x: auto">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="width: 90%">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商铺标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入商铺标题"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="浏览量" prop="pageViews">
                <el-input v-model="form.pageViews" placeholder="请输入浏览量"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="栏目分类" prop="columnType">
                <el-select v-model="form.columnType" clearable placeholder="请选择栏目分类" style="width: 100%">
                  <el-option
                    v-for="item in columnType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属类别" prop="classType">
                <el-select v-model="form.classType" clearable placeholder="请选择所属类别" style="width: 100%">
                  <el-option
                    v-for="item in classType"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属区域" prop="region">
                <el-select v-model="form.region" clearable placeholder="请选择所属区域" style="width: 100%"
                           @change="updateRegion">
                  <el-option
                    v-for="item in regionType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发布时间" prop="releaseDatetime">
                <el-date-picker v-model="form.releaseDatetime"
                                clearable
                                placeholder="请选择发布时间"
                                style="width: 100%"
                                type="date"
                                value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contacts">
                <el-input v-model="form.contacts" placeholder="请输入联系人"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入联系方式"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="装修程度" prop="decoration">
                <el-input v-model="form.decoration" placeholder="请输入装修程度"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="租金" prop="rent">
                <el-input-number v-model="form.rent" :controls="false" label="请输入租金"
                                 style="width: 100%; text-align: left!important;"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="面积(㎡)" prop="area">
                <el-input-number v-model="form.area" :controls="false" label="请输入面积(㎡)"
                                 style="width: 100%; text-align: left!important;"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="店铺属性" prop="attribute">
            <el-checkbox-group v-model="form.attribute">
              <el-checkbox
                v-for="item in attributeType"
                :key="item.value"
                :label="item.value"
              >{{ item.label }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="审核" prop="review">
            <el-radio-group v-model="form.review">
              <el-radio
                v-for="dict in dict.type.xds_review"
                :key="dict.value"
                :label="dict.value"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-row>
            <el-col :span="6">
              <el-form-item label="经度" prop="longitude">
                <el-input v-model="form.longitude" placeholder="请输入经度" @change="updateLatLng"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="维度" prop="dimension">
                <el-input v-model="form.dimension" placeholder="请输入维度" @change="updateLatLng"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="快速检索" prop="dimension">
                <el-select v-model="name" :remote-method="remoteMethod2" class="search-input" clearable filterable
                           placeholder="请输入" remote
                           style="width: 100%" value-key="id" @change="onChange2">
                  <el-option v-for="item in addressList" :key="item.id" :label="item.title" :value="item">
                    <span>{{ item.title }}</span>
                    <span style="float: right; color: #8492a6; font-size: 12px">{{ item.address }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div style="margin-left: 30px;">
                <!-- 地图选点 -->
                <mapSelect v-if="open" ref="mapSelectRef" :fatherName="val"
                           @update-coordinates="handleCoordinatesUpdate"></mapSelect>
              </div>
            </el-col>
          </el-row>


          <el-row>
            <el-col :span="12">
              <el-form-item label="缩略图" prop="img">
                <image-upload v-model="form.img" :limit="1"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <vue-qr :size="170" :text="qrCodeUrl"></vue-qr>
              <div class="el-upload__tip" style="margin-left: 30px; margin-top: -20px">
                扫描二维码上传图片
              </div>
            </el-col>
          </el-row>
          <el-form-item label="图片相册" prop="imgurl">
            <image-upload v-model="form.imgurl" :limit="8"/>
          </el-form-item>
          <el-form-item label="附近地标" prop="landmarks">
            <el-input v-model="form.landmarks" placeholder="请输入内容" type="textarea"/>
          </el-form-item>
          <el-form-item label="配套设施" prop="facilities">
            <el-input v-model="form.facilities" placeholder="请输入内容" type="textarea"/>
          </el-form-item>
          <el-form-item label="内容描述">
            <editor v-model="form.remark" :min-height="192"/>
          </el-form-item>
        </el-form>
      </div>
      <div class="demo-drawer__footer">
        <el-button style="width: 40% !important; margin-left: 10%" type="primary" @click="submitForm">确 定</el-button>
        <el-button style="width: 40% !important;" @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {addShop, getShop, listShop, updateDel, updateShop} from "@/api/xds/shop"
import {listColumn} from "@/api/xds/column";
import {listClass} from "@/api/xds/class";
import {listAttribute} from "@/api/xds/attribute";
import {listRegion} from "@/api/xds/region";
import mapSelect from '@/views/xds/ditu/index.vue'
import {parseTime} from "@/utils/ruoyi";
import VueQr from 'vue-qr/src/packages/vue-qr.vue';

export default {
  components: {
    mapSelect,
    VueQr,
  },
  dicts: ['xds_review'],
  name: "Shop",
  data() {
    return {
      qrCodeUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺管理表格数据
      shopList: [],
      // 栏目管理表格数据
      columnType: [],
      //类型管理
      classType: [],
      //店铺属性
      attributeType: [],
      //区域
      regionType: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addressList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        title: null,
        classType: null,
        columnType: null,
        review: null,
        pageViews: null,
        region: null,
        attribute: null,
        img: null,
        imgurl: null,
        releaseDatetime: null,
        longitude: null,
        dimension: null,
        address: null,
        contacts: null,
        phone: null,
        decoration: null,
        rent: null,
        area: null,
        landmarks: null,
        facilities: null,
        isdel: 0,
      },
      // 表单参数
      name: '',
      form: {
        review: 0,
        attribute: ['1']
      },
      val: {
        location: {
          lat: 0,
          lng: 0,
        }
      },
      // 表单校验
      rules: {
        title: [
          {required: true, message: "请输入商铺标题", trigger: "blur"}
        ],
        columnType: [
          {required: true, message: "请选择栏目分类", trigger: "blur"}
        ],
        classType: [
          {required: true, message: "请选择所属类别", trigger: "blur"}
        ],
        region: [
          {required: true, message: "请选择所属区域", trigger: "blur"}
        ],
        releaseDatetime: [
          {required: true, message: "请选择发布时间", trigger: "blur"}
        ],
        contacts: [
          {required: true, message: "请输入联系人", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "请输入联系方式", trigger: "blur"}
        ],
        decoration: [
          {required: true, message: "请输入装修程度", trigger: "blur"}
        ],
        rent: [
          {required: true, message: "请输入租金", trigger: "blur"}
        ],
        address: [
          {required: true, message: "请输入地址", trigger: "blur"}
        ],
        area: [
          {required: true, message: "请输入面积", trigger: "blur"}
        ],
        attribute: [
          {required: true, message: "请输入店铺属性", trigger: "blur"}
        ],
        review: [
          {required: true, message: "请选择审核状态", trigger: "blur"}
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getColumnList()
    this.getClassList()
    this.getAttribute()
    this.getRegion()
  },
  methods: {
    // 位置模糊搜索
    remoteMethod2(query) {
      if (query != '') {
        this.getAddress(query);
      } else {
        this.addressList = [];
      }
    },
    getAddress(query) {
      let that = this
      this.$jsonp("https://apis.map.qq.com/ws/place/v1/suggestion/", {
        key: 'DMFBZ-MDC6Q-IRV5V-2XVI2-NQDQV-OOFUU',
        keyword: query,
        region: '徐州',
        output: 'jsonp',
      }).then(res => {
        this.addressList = res.data
      }).catch(err => {
        console.log("catch", err);
      });
    },
    // 地址选择
    onChange2(val) {
      if (!val.location) return; // 数据异常时跳过
      // 更新地图中心点
      this.$refs.mapSelectRef.initMap(val); // 调用子组件方法
    },
    parseTime,
    handleCoordinatesUpdate(data) {
      this.form.longitude = data.lng
      this.form.dimension = data.lat
    },
    /** 查询省市区列表 */
    async getRegion() {
      this.loading = true
      const data = {
        pid: 842,
        pageNum: 1,
        pageSize: 100,
      }
      let that = this;
      await listRegion(data).then(response => {
        that.regionType = response.rows
      })
    },
    /** 获取店铺属性 */
    async getAttribute() {
      this.loading = true
      const data = {
        isdel: 0,
        pageNum: 1,
        pageSize: 100,
      }
      let that = this;
      await listAttribute(data).then(response => {
        for (let i = 0; i < response.rows.length; i++) {
          that.attributeType.push({value: response.rows[i].id, label: response.rows[i].title})
        }
      })
    },
    /** 获取栏目管理 */
    async getColumnList() {
      this.loading = true
      const data = {
        parentId: 2,
        isdel: 0,
        pageNum: 1,
        pageSize: 100,
      }
      let that = this;
      await listColumn(data).then(response => {
        for (let i = 0; i < response.rows.length; i++) {
          that.columnType.push({value: response.rows[i].deptId, label: response.rows[i].deptName})
        }
      })
    },
    /** 获取类别管理 */
    async getClassList() {
      this.loading = true
      const data = {
        pageNum: 1,
        pageSize: 100,
      }
      let that = this;
      listClass(data).then(response => {
        for (let i = 0; i < response.rows.length; i++) {
          that.classType.push({value: response.rows[i].id, label: response.rows[i].title})
        }
      })
    },

    /* 修改所属区域 */
    updateRegion() {
      // 查找 regionType 中 id 为 842 的元素
      const selectedRegion = this.regionType.find(item => item.id === this.form.region);
      const val = {
        location: {
          lat: selectedRegion.lat,
          lng: selectedRegion.lng,
        }
      }
      this.$refs.mapSelectRef.initMap(val)
    },

    /** 查询商铺管理列表 */
    getList() {
      this.loading = true
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        classType: null,
        columnType: null,
        pageViews: null,
        region: null,
        img: null,
        imgurl: null,
        releaseDatetime: null,
        longitude: null,
        dimension: null,
        address: null,
        contacts: null,
        phone: null,
        decoration: null,
        rent: null,
        area: null,
        landmarks: null,
        facilities: null,
        remark: null,
        isdel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        review: '0',
        attribute: [1]
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form.longitude = this.regionType[0].lng
      this.form.dimension = this.regionType[0].lat
      this.form.region = this.regionType[0].id
      this.val.location.lng = this.regionType[0].lng
      this.val.location.lat = this.regionType[0].lat
      this.open = true
      this.title = "添加商铺管理"
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      await getShop(id).then(response => {
        this.form = response.data
        this.form.attribute = this.form.attribute.split(",").map(Number)
        this.form.review = this.form.review.toString()
        this.open = true
        this.title = "修改商铺管理"
        this.val.location.lng = this.form.longitude
        this.val.location.lat = this.form.dimension
        this.$refs.mapSelectRef.initMap(this.val)
      })
    },
    updateLatLng() {
      this.val.location.lng = this.form.longitude
      this.val.location.lat = this.form.dimension
      this.$refs.mapSelectRef.initMap(this.val)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.attribute = this.form.attribute.join(",")
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除商铺管理编号为"' + row.id + '"的数据项？').then(function () {
        return updateDel(row)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.el-input__inner {
  text-align: left !important;
}
</style>
